<template>
	<view>
		<view class="loading-view">
			<u-loading mode="circle" color="blue" size="100"></u-loading>
			<view class="loading-label">认证中...</view>
		</view>
	</view>
</template>

<script>
	import utils from '../../common/utils.js'
	import config from '../../common/config.js'
	import {
		qywxWebLogin
	} from '../../common/service.js'

	export default {
		data() {
			return {
				appid: "",
				lesseeid: "",
				code: "",
				user: {},
				token: "",
				state: ""
			}
		},
		methods: {
			getAppId() {
				let appid = utils.getQueryString('appid')
				if (!appid) {
					appid = utils.getSession('appid')
				} else {
					utils.setSession('appid', appid)
					config.appid = appid
				}
				return appid
			},

			getLesseeId() {
				let lesseeid = utils.getQueryString("lesseeid")
				if (!lesseeid) {
					lesseeid = utils.getSession('lesseeid')
				} else {
					utils.setSession('lesseeid', lesseeid)
					config.lesseeid = lesseeid
				}
				return lesseeid
			},

			getState() {
				let state = window.atob(utils.getQueryString("state"))
				return state.replace("#", "")
			},

			getCode() {
				return utils.getQueryString("code")
			},

			doAuth() {
				let that = this
				if (!that.lesseeid || !that.code) {
					return
				}
				qywxWebLogin({
					lesseeid: that.lesseeid,
					code: that.code
				}, function(res) {
					if (res && res.data.code == 200) {
						// user = {
						// 	id: "HN036505",
						// 	name: "苏文柔",
						// 	position: "前端开发工程师",
						// 	dept: "信息部技术开发部",
						// 	rp02: "",
						// 	rp03: "",
						// 	brand: "",
						// 	storeName: "",
						// 	storeAddress: "",
						// 	phone: "13249425230",
						// 	avatar: "https://wework.qpic.cn/wwhead/duc2TvpEgSTPk74IwG7Bs1eenc6tmSFS3vphgia4d9M4H7xeu4cCtPF714ib7ukdsv2V24Wib6ffzE/0",
						// 	external_userid: "wmiQjXCQAAKyEv1JSBv3d5DcYiePv7IQ"
						// };
						// 后续鉴权, 需要看看一二级销区和品牌该怎么取值
						that.user = {
							id: res.data.qy_user.userid || "",
							name: res.data.qy_user.name || "",
							position: res.data.qy_user.position || "",
							dept: res.data.qy_user.main_department || "",
							phone: res.data.qy_user.mobile || "",
							avatar: res.data.qy_user.avatar || "",
							external_userid: res.data.qy_user.external_userid || ""
						}
						that.token = res.data.token
						utils.setSession('user', that.user)
						utils.setSession('token', that.token)
						config.user = that.user
						config.token = that.token
						uni.redirectTo({
							url: that.state
						})
					} else {
						alert(res.data.msg)
					}
				})
			}
		},
		onLoad: function() {
			this.appid = this.getAppId()
			this.lesseeid = this.getLesseeId()
			this.state = this.getState()
			this.code = this.getCode()
			this.doAuth()
		}
	}
</script>

<style scoped>
	.loading-view {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -100%);
		text-align: center;
	}

	.loading-view .loading-label {
		margin-top: 40rpx;
		font-family: PingFang-SC-Regular;
		font-size: 40rpx;
		font-weight: normal;
		font-stretch: normal;
		line-height: 34rpx;
		letter-spacing: 0px;
	}
</style>
